package github.hellocsl.gallerylayoutmanager;

import github.hellocsl.gallerylayoutmanager.layout.impl.CurveTransformer;
import github.hellocsl.gallerylayoutmanager.util.ToastUtil;
import github.hellocsl.layoutmanager.gallery.Banner;
import io.alterac.blurkit.BlurKit;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class ViewPagerFraction extends Fraction {
    private Image imageBg;
    private Banner banner;
    private final List<Integer> listData = new ArrayList<>();

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component mComponent = scatter.parse(ResourceTable.Layout_fraction_view_pager, container, false);
        init(mComponent);
        return mComponent;
    }

    /**
     * 初始化
     *
     * @param mComponent 布局组件
     */
    private void init(Component mComponent) {
        mComponent.setClickedListener(component -> {});
        imageBg = (Image) mComponent.findComponentById(ResourceTable.Id_image_viewpager_bg);
        banner = (Banner) mComponent.findComponentById(ResourceTable.Id_banner);

        List<Integer> mResId = new ArrayList<>();
        mResId.add(ResourceTable.Media_img1);
        mResId.add(ResourceTable.Media_img2);
        mResId.add(ResourceTable.Media_img3);
        mResId.add(ResourceTable.Media_img4);

        listData.clear();
        for (int i = 0; i < 50; i++) {
            listData.add(mResId.get(i % mResId.size()));
        }

        Optional<Display> displayManager = DisplayManager.getInstance().getDefaultDisplay(getFractionAbility());
        int width = displayManager.get().getAttributes().width;
        int height = displayManager.get().getAttributes().height;

        // 设置界面切换动画
        banner.setPageTransformer(new CurveTransformer());
        banner.setPageMargin((int) -(width - (width * 0.7f)));
        banner.setPageCacheSize(2);
        banner.setData(listData, (int) (width * 0.7f), (int) (height * 0.8f));
        banner.setPageClickedListener((component, position) -> {
            ToastUtil.getInstance().showToast(getFractionAbility(), "clickitem:" + position);
            if(position!=banner.getCurrentPage()){
                banner.setCurrentPage(position);
            }
        });
        banner.setPageSelectedListener((component, position) -> {
            imageBg.setPixelMap(listData.get(position));
            imageBg.setPixelMap(BlurKit.getInstance().fastBlur(imageBg, 2));
        });
        banner.setCurrentPage(30, false);
        imageBg.setPixelMap(listData.get(0));
        imageBg.setPixelMap(BlurKit.getInstance().fastBlur(imageBg, 2));
    }
}
